<?php

namespace App\Repository;

use App\Models\SpiderContent;
use App\Models\SpiderInfo;
use App\Models\SpiderOriginal;
use Illuminate\Database\Eloquent\Builder;

/**
 * Class SpiderRepository
 * @package App\Repository
 */
class SpiderRepository extends BaseRepository
{

    /**
     * 新增[spider_content]
     * @param $loadData
     * @return false|SpiderContent
     */
    public function createSpiderContent($loadData)
    {
        $spiderContent = $this->getSpiderContent($loadData['url']);
        if (!$spiderContent) {
            $spiderContent = SpiderContent::getModel()->create($loadData);
            return $spiderContent ?: false;
        }
        return $spiderContent;
    }

    /**
     * 获取[spider_content]
     * @param $url
     * @return SpiderContent|null
     */
    public function getSpiderContent($url)
    {
        return SpiderContent::getModel()
            ->where('url', $url)
            ->first();
    }

    /**
     * 通过链接获取爬虫信息
     * @param $url
     * @param string $selects
     * @return Builder|SpiderInfo|object|null
     */
    public function getSpiderInfoByUrl($url, $selects = ['*'])
    {
        return SpiderInfo::getInstance()
            ->select($selects)
            ->where('url', $url)
            ->first();
    }

    /**
     * 通过来源ID获取来源信息
     * @param $spiderOriginalId
     * @param string $selects
     * @return Builder|SpiderOriginal|object|null
     */
    public function getSpiderOriginalByUrl($spiderOriginalId, $selects = ['*'])
    {
        return SpiderOriginal::getInstance()
            ->select($selects)
            ->where('id', $spiderOriginalId)
            ->first();
    }

}
